import Reqwest from '../../utils/Reqwest';
import {TagBean} from './TagBean';
import {base, assign, redirect} from '../base/Base';
import {ResponseDto} from '../base/BaseDTO';
import {TagSearchBean} from "./TagSearchBean";

declare let GLOBAL: any;

export class TagController {

    path = `${GLOBAL.baseApiUrl}/adTag/api`;

    addTag(params: TagBean<any>, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/add.jhtml`,
            method: 'post',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(params) || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * 删除标签.
     *
     * @return
     */
    deleteTag(params?: { tagId: number }, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/delete.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     *更新标签.
     *
     * @return
     */
    editTag(params: TagBean<any>, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/update.jhtml`,
            method: 'post',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(params) || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }


    /**
     *更新标签.
     *
     * @return
     */
    checkTag(params: {id:number}, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/view.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * 标签列表.
     *
     * @return
     */
    searchTag(params?: TagSearchBean<any>, options = {}): Promise<any> {
        return new Reqwest(assign({
            url: `${this.path}/list.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * 根据应用名称查询应用.
     *
     * @return
     */
    getApps(params?: { query: string }, options = {}): Promise<ResponseDto<TagBean<any>>> {
        return new Reqwest(assign({
            url: `${this.path}/apps.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }


}